package javafx.officialdemo.RadioButton;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class FXRadioButton extends Application{
	/**
	 * ToggleButton 的一种特定实现，所以这里先描述 ToggleButton 
	 * 再 描述  RadioButton
	 * 
	 * clicking the selected toggle button causes it to become deselected, 
	 * clicking the selected radio button in the group has no effect.
	 */
	
	/*--------------------ToggleButton-----------------------*/
	/**
	 * 构造器
	 * 
		//A toggle button without any caption or icon
		ToggleButton tb1 = new ToggleButton();
		//A toggle button with a text caption
		ToggleButton tb2 = new ToggleButton("Press me");
		//A toggle button with a text caption and an icon
		Image image = new Image(getClass().getResourceAsStream("icon.png"));
		ToggleButton tb3 = new ToggleButton ("Press me", new ImageView(image));	 
	 */
	
	/**
	 * 将toggle button加入一个组
	 * 
		ToggleGroup group = new ToggleGroup();
		
		ToggleButton tb1 = new ToggleButton("Minor");
		tb1.setToggleGroup(group);
		tb1.setSelected(true);
		
		ToggleButton tb2 = new ToggleButton("Major");
		tb2.setToggleGroup(group);
		
		ToggleButton tb3 = new ToggleButton("Critical");
		tb3.setToggleGroup(group);
	 */
	
	/**
	 * 一个例子
	 * 点击 按钮改变 矩形颜色
	 *
		tb1.setUserData(Color.LIGHTGREEN);
		tb2.setUserData(Color.LIGHTBLUE);
		tb3.setUserData(Color.SALMON);
	
		Rectangle rect = new Rectangle();
		rect.setHeight(50);
		rect.setFill(Color.WHITE);
		rect.setStroke(Color.DARKGRAY);
		rect.setStrokeWidth(2);
		rect.setArcHeight(10);
		rect.setArcWidth(10);
	
		final ToggleGroup group = new ToggleGroup();
	
		group.selectedToggleProperty().addListener            
		    (ObservableValue<? extends Toggle> ov, 
		    Toggle toggle, Toggle new_toggle) -> {
		    if (new_toggle == null)
		        rect.setFill(Color.WHITE);
		    else
		        rect.setFill((Color) group.getSelectedToggle().getUserData());
		});
	
		rect.setWidth(hbox.getWidth());
	*/
	
	/**
	 * 增加 效果  css
	 * css：
	 * .toggle-button1{-fx-base: lightgreen;   }
	 * 
	 * tb1.getStyleClass().add("toggle-button1");
	 */
	
	/*--------------------RadioButton-----------------------*/
	
	/**
	 * 构造器
	 * 
	 * A radio button with an empty string for its label
	 * RadioButton rb1 = new RadioButton();
	 * 
	 * Setting a text label
	 * rb1.setText("Home");
	 * 
	 * A radio button with the specified label
	 * RadioButton rb2 = new RadioButton("Calendar");
	 * 
	 * Image image = new Image(getClass().getResourceAsStream("ok.jpg"));
	 * RadioButton rb = new RadioButton("Agree");
	 * rb.setGraphic(new ImageView(image));
	 */
	
	/**
	 * 将radio button 加入一个组
	 * 
	 * 
		ToggleGroup group = new ToggleGroup();
		RadioButton rb1 = new RadioButton("Home");
		rb1.setToggleGroup(group);
		rb1.setSelected(true);
		
		RadioButton rb2 = new RadioButton("Calendar");
		rb2.setToggleGroup(group);
		 
		RadioButton rb3 = new RadioButton("Contacts");
		rb3.setToggleGroup(group);	
	 */
	
	/**
	 * 增加监听事件
	 */
	RadioButton rb1 = new RadioButton("Home");
	RadioButton rb2 = new RadioButton("Calendar");
	RadioButton rb3 = new RadioButton("Contacts");
	public ToggleGroup test(){
		final ToggleGroup group = new ToggleGroup();
		rb1.setToggleGroup(group);
		rb1.setSelected(true);
		rb2.setToggleGroup(group);
		rb3.setToggleGroup(group);	
		
		rb1.setUserData("这是第一个按钮");
		rb2.setUserData("这是第二个按钮");
		rb3.setUserData("这是第三个按钮");
		
		group.selectedToggleProperty().addListener(new ChangeListener() {
			/**
			 * 监听方法
			 * @param arg0	ReadOnlyObjectProperty 
			 * 				[value: RadioButton@1227ce4[styleClass=radio-button]'Contacts']
			 * @param arg1		旧的选项
			 * @param arg2		新的选项
			 */
			public void changed(ObservableValue arg0, Object arg1, Object arg2) {
				 Object userData = group.getSelectedToggle().getUserData();
				 System.out.println(userData);
				 
				 System.out.println(arg0);
				 System.out.println(arg1);
				 System.out.println(arg2);
			}
		});
		
		return group;
	}

	public void start(Stage primaryStage) throws Exception {
		GridPane root = new GridPane();
		
		ToggleGroup test = test();
		root.add(rb1,0,0);
		root.add(rb2,0,1);
		root.add(rb3,0,2);
		
		Scene scene = new Scene(root, 300, 250);
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	
	public static void main(String[] args) {
		launch(null);
	}
	
	/**
	 * 设置选择 
	 * rb2.setSelected(true);
	 * 
	 * 设置强制取得 焦点 
	 * rb2.requestFocus();
	 * 
	 */
}